﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Temp.Database;
using Temp.Model;

namespace Temp.Controller
{
    class CharacterController
    {
        private SQLiteFactory sqlf;
        private List<Character> characterList;
        private List<Ontology> characterOnt;
        private SQLite.SQLiteConnection conn;

        public CharacterController()
        {
            sqlf = new SQLiteFactory();
        }

        public List<Character> getNPCs()
        {
            characterList = new List<Character>();
            PlaceController pCtr = new PlaceController();

            conn = sqlf.getConnection();
            characterList = conn.Query<Character>("SELECT *  FROM Character").ToList();
            String str = "";
            foreach (Character ch in characterList)
            {
                str = conn.Query<Ontology>("SELECT *  FROM Character_Ont where concept1='" + ch.name + "' and relation='locatedAt'").Single().concept2;
                ch.idPlace = pCtr.getPlaceIdByString(str);
            }
            conn.Close();
            return characterList;
        }
        
        //public List<Rectangle> allNPCRect(string placeName)
        //{
        //    List<Character> listchar = getNPCs(placeName);
        //    List<Rectangle> rects = new List<Rectangle>();

        //    for (int i = 0; i < listchar.Count; i++)
        //        rects.Add(new Rectangle(listchar.ElementAt(i).x, listchar.ElementAt(i).y,
        //           listchar.ElementAt(i).Width, listchar.ElementAt(i).Height)); //temporary size

        //    return rects;
        //}


        public List<Ontology> getAttributeValues(string tag, string name)
        {
            conn = sqlf.getConnection();
            List<Ontology> cOntList = conn.Query<Ontology>("SELECT * FROM Character_Ont WHERE relation = ? AND concept1 = ? COLLATE NOCASE", tag, name).ToList();
            conn.Close();
            return cOntList;
        }

        public List<Ontology> getStoryNodeOptionUsed(string optionName) {
            conn = sqlf.getConnection();
            List<Ontology> o = conn.Query<Ontology>("SELECT * FROM Character_Ont WHERE relation = ? AND concept1 = ?", "usedDuring", optionName).ToList();
            conn.Close();
            return o;
        }

        public List<Ontology> getValues(string optionName)
        {
            conn = sqlf.getConnection();
            List<Ontology> o = conn.Query<Ontology>("SELECT * FROM Character_Ont WHERE relation = ? AND concept1 = ?", "consistsOf", optionName).ToList();
            conn.Close();
            return o;
        }
    }
}
